package cn.tedu.egweibo1.controller;

import cn.tedu.egweibo1.mapper.WeiboMapper;
import cn.tedu.egweibo1.pojo.dto.WeiboDTO;
import cn.tedu.egweibo1.pojo.entity.Weibo;
import cn.tedu.egweibo1.pojo.vo.UserVO;
import cn.tedu.egweibo1.pojo.vo.WeiboDetailVO;
import cn.tedu.egweibo1.pojo.vo.WeiboIndexVO;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @PostMapping("insert")
    public int insert(@RequestBody WeiboDTO weiboDTO, HttpSession session) {
        System.out.println("weiboDTO:" + weiboDTO);
        UserVO userVO=(UserVO) session.getAttribute("user");
        if(userVO==null){
            return 2;
        }
        Weibo weibo=new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setUserId(userVO.getId());
        weiboMapper.insert(weibo);
        return 1;
    }

    @GetMapping("selectIndex")
    public List<WeiboIndexVO> selectIndex(){
        return weiboMapper.selectIndex();
    }
    @GetMapping("selectById")
    public WeiboDetailVO selectById(Long id){
        return weiboMapper.selectById(id);
    }
}
